<%@page language="java" contentType="text/html;charset=UTF-8" import="java.sql.*" import="java.lang.Object" import="java.util.Date" import="java.security.MessageDigest"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="Annotation.Cluster.Methods.*"%>
<%@ page import="java.io.*"%>
<%@page import="Annotation.DataBase.*"%>
<%@ page import="java.util.*"%>
<%
String ClusterNumber = request.getParameter("cn");
String method = request.getParameter("method");
int methodID = Integer.parseInt(method);
//String readingActitityID = "8";
int readingActitityID=11;
%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Update the quest result</title>
		<style type="text/css">
			th {text-align:center; vertical-align:top;}
			.source{width:250px; height:400px; padding:10px; border:1px solid #aaaaaa; background:cyan; box-shadow: 10px 10px 5px #dddddd;}
			.divbox{width:250px; height:70px; padding:10px; border:1px solid #aaaaaa; box-shadow: 10px 10px 5px #888888;}
			.items{width:200px; border:2px solid #a1a1a1; padding:5px 5px; background:#dddddd;}
		</style>

	</head>
	<body>
		<hr>
		
<% //item10-div0###item12-div0###item11-div2###item9-div2###
	MySQLCommand ss= new MySQLCommand();

	//String sqls="SELECT cluster_student_list,updateTime FROM cluster_info WHERE PK_reading_activity = "+readingActitityID+" AND PK_cluster_approach = "+methodID+";";
	String sqls="SELECT cluster_student_list,updateTime FROM cluster_info WHERE PK_reading_activity = "+readingActitityID+" AND PK_cluster_approach = "+methodID+";";
	
	
	ResultSet rss = ss.selectSQL(sqls);

	//out.println(sqls);
	out.println("此頁面是為使用原始資料計算");
				
	out.println("reading ID==="+readingActitityID);

	if(rss.next()){
		//has next(), has data already
		if(rss.getString("cluster_student_list")==null){
				
				ArrayList<ArrayList> treeInfo = new ArrayList<ArrayList>();
				//treeInfo.add(firstLeafID); //int for first level leaf - all users
				//treeInfo.add(treelist); //double[4] tree and its leaf  -- SysID, DB_PK, X, Y
				//treeInfo.add(leaflist); //int[3] ever leaf is emery which two leaf -- SysID, merge1, merge2
				ArrayList<Integer> firstLeafID = new ArrayList<Integer>();
				ArrayList<double[]> treelist = new ArrayList<double[]>();
				ArrayList<int[]> leaflist = new ArrayList<int[]>();
				ArrayList<double[]> al = new ArrayList<double[]>();
				
				StartMethodForGroupNum methods=new StartMethodForGroupNum(Integer.parseInt(ClusterNumber));
				
				switch(methodID){
				case 1:
					treeInfo=methods.getStandarTree();
					//firstLeafID=treeInfo.get(0);
					leaflist=treeInfo.get(2);
					al=treeInfo.get(3);
					out.println("Standard");
					break;
				case 2:
					treeInfo=methods.getQuantitativeTree();
					//firstLeafID=treeInfo.get(0);
					leaflist=treeInfo.get(2);
					al=treeInfo.get(3);
					out.println("Quantitative");
					break;
				case 3:
					treeInfo=methods.getCosineTree();
					//firstLeafID=treeInfo.get(0);
					leaflist=treeInfo.get(2);
					al=treeInfo.get(3);
					out.println("Cosine");
					break;
				case 4:
					treeInfo=methods.getDiffusionTree();
					//firstLeafID=treeInfo.get(0);
					leaflist=treeInfo.get(2);
					al=treeInfo.get(3);
					out.println("Diffusion");
					break;
				}
				out.println("上次更新時間:"+rss.getString("updateTime"));
				
%>
		
		
		<table width="600" border="1">			
			<tr>
				<th width="50%" style="padding:5px;">
					<%	
					String stuList="";
					for(int i=0;i<al.size();i++){  //產生div的地方
						String notStore="";
						out.print("<div id='div" + i + "' class='divbox'>");
						
						double nodeValue = al.get(i)[0];
						
						if(nodeValue>0){ //表示是葉節點
							String nv = nodeValue+"";
							nv=nv.substring(0, nv.indexOf("."));
							out.print("<div id='item" + nv + "' class='items'>");
							out.print("第"+nv+"個項目");
							out.print("</div>");
							stuList = stuList + "item"+ nv +"-"+ "div"+i+"###";
						}else if(nodeValue<0){ //表示是合併節點
							//checkNumber(leaflist, nodeValue);
							String node=checkTreeLeaf(leaflist,nodeValue);
							//System.out.println("node====="+node);
							String mm[]=node.split("@@&&");
							for(int k =0;k<mm.length;k++){
								//System.out.println("K=="+mm[k]);
								out.print("<div id='item" + mm[k] + "' class='items'>");
								out.print("第"+mm[k]+"個項目");
								out.print("</div>");
								stuList = stuList + "item"+ mm[k] +"-"+ "div"+i+"###";
							}
						}
						out.print("</div><p>");
						
					}
					//out.print("<br>"+stuList);
					%>
					<INPUT type=hidden name=info id=info value="<%=stuList%>">
				</th>
				
			</tr>				
		</table>	

		<table width="600" border="1">			
			<tr>
				
				<th width="50%" style="padding:5px;">
	    			<input type="button" id="saveResult" name="saveResult" onClick="" value="Save Result"/>
	    			<input type="button" id="createCluster" name="create" onClick="numberAdding(event)" value="Edit clusters"/>
	    		</th>
				
			</tr>		
						
		</table>	
		
		
<%


			//out.println(timeStamp());
			//sqls="UPDATE cluster_info SET cluster_student_list = '"+stuList+"', updateTime = '"+timeStamp()+"' WHERE PK_reading_activity = "+readingActitityID+" AND PK_cluster_approach = "+methodID+";"; 
			sqls="UPDATE cluster_info SET cluster_student_list = '"+stuList+"', updateTime = '"+timeStamp()+"' WHERE PK_reading_activity = "+readingActitityID+" AND PK_cluster_approach = "+methodID+";"; 

			//out.println(sql+"<br>");
			ss.executeSQL(sqls);
			

		}//if 資料庫裡沒有資料的話執行以上
		else{
			response.sendRedirect("reviewClusterResult3_showClusterResultInDB.jsp?method="+methodID+"&readingActitityID="+readingActitityID);
													
		}
		
	}else{
		//false，doesn't have data
		out.println("沒有資料--照理說應該是不會出現這行");
			
	}
	ss.closeDB();
%>


				<%!
				private String timeStamp(){
					Calendar thisMonth=Calendar.getInstance(); 
					//日期格式 年-月-日-時-分-秒
					String date=thisMonth.get(Calendar.YEAR)+"-"+(thisMonth.get(Calendar.MONTH)+1)+"-"+thisMonth.get(Calendar.DAY_OF_MONTH)+"-"+thisMonth.get(Calendar.HOUR)+"-"+thisMonth.get(Calendar.MINUTE)+"-"+thisMonth.get(Calendar.SECOND);
					return date;
				}
				%>
		
				<%!
				private String checkTreeLeaf(ArrayList<int[]> leaflist, double nodValue){
					String s="";
					for(int i=0;i<leaflist.size();i++){ //array 裡全部巡一回找出nodeValue的資料
						if(nodValue==leaflist.get(i)[0]){ //找到node的資料後要幹麻?
							
							int ch=leaflist.get(i)[1];
							if(ch>0){
								s=s+ch+"@@&&";
							}else{
								s=s+checkTreeLeaf(leaflist,ch);
							}	
							
							int ch2=leaflist.get(i)[2];
							if(ch2>0){
								s=s+ch2+"@@&&";	
							}else{
								s=s+checkTreeLeaf(leaflist,ch2);
							}
						}
					}
					return s;
				}
				%>
	</body>
</html>